<template>
  <a-drawer
    title="分数线设置"
    width=450
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="addVisible"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-row :gutter="48">
        <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['zsyxm',{initialValue:postParams.zsyxm}]" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="专业">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['zszym',{initialValue:postParams.zszym}]">
                <a-select-option v-for="i in selectMenu.zy" :key="i.id" :value="i.zyh">{{ i.zyh+'-'+i.zymc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
           <a-col :sm="{span:12}" :lg="{span:12}"  >
           <a-form-item label="招生年度">
             <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.zsnj" >
               <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
             </a-select>
           </a-form-item>
         </a-col>

          <a-col :sm="{span:12}" :lg="{span:12}"  >
           <a-form-item label="学习形式">
             <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="postParams.isqrz" @change="xxChange">
               <a-select-option v-for="i in selectMenu.xxlx" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
             </a-select>
           </a-form-item>
         </a-col>
         <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="研究方向">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-decorator="['yjfxm',
                  {
                      rules: [{ required: true, message: '请输入研究方向!' }],
                  },{initialValue:postParams.yjfxm}]" >
                <a-select-option v-for="i in selectMenu.fx" :key="i.yjfxm" :value="i.yjfxm">{{ i.yjfxm.substring(i.yjfxm.length-2) +"-"+ i.yjfxmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目一">
                <a-input placeholder="请输入..." v-model='postParams.kmyfsx' @change="fsChange"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目二">
                <a-input placeholder="请输入..." v-model='postParams.kmefsx' @change="fsChange"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目三">
                <a-input placeholder="请输入..." v-model='postParams.kmsfsx' @change="fsChange"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="科目四">
                <a-input placeholder="请输入..." v-model='postParams.kmffsx' @change="fsChange"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}"  >
            <a-form-item label="总分">
                <!-- <a-input placeholder="请输入..." v-decorator="['zffsx',{initialValue:postParams.zffsx}]"/> -->
                <a-input placeholder="请输入..."  v-model='zffsx'/>
            </a-form-item>
          </a-col>
      </a-row>
    </a-form>
    <div class="drawer-bootom-button">
      <a-button style="margin-right: .8rem" @click="onClose">取消</a-button>
      <a-button @click="handleSubmit" type="primary" :loading="loading">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
// import moment from 'moment'
import { STable } from '~'
import { mapState } from 'vuex'

const formItemLayout = {
  labelCol: { span: 3 },
  wrapperCol: { span: 18 }
}

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']
export default {
  props: {
    addVisible: {
      default: false
    }
  },
  components: {
    STable
  },
  data () {
    return {
      formItemLayout,
      loading: false,
      postParams: {
        zsyxm:"",
        zszym:"",
        isqrz:"",
        kmyfsx:0,
        kmefsx:0,
        kmsfsx:0,
        kmffsx:0,
        zffsx:0,
        zsnj:''
      },
      zffsx:0,
      selectMenu: {
        yx: [] ,// 院系
        zy:[],
        xxlx:[],
      }
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })

  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.postParams.zsyxm =  res[0].data[0].dwh
        this.getProssion(res[0].data[0].dwh)
        this.$api.base.dictionaryData('TYPE0441').then(res => {
        	this.selectMenu.xxlx = res.data
        })

      })
    },
    reset () {
      this.data = []
    },
    fsChange(){
       var total = 0
       if(parseInt(this.postParams.kmyfsx)){
         total +=parseInt(this.postParams.kmyfsx)
       }
      if(parseInt(this.postParams.kmefsx)){
        total +=parseInt(this.postParams.kmefsx)
      }
      if(parseInt(this.postParams.kmsfsx)){
        total +=parseInt(this.postParams.kmsfsx)
      }
      if(parseInt(this.postParams.kmffsx)){
        total +=parseInt(this.postParams.kmffsx)
      }
      // var total = parseInt(this.postParams.kmyfsx) + parseInt(this.postParams.kmefsx) +parseInt(this.postParams.kmsfsx)+parseInt(this.postParams.kmffsx)
      this.zffsx= total


    },
     // 选择院系
    yxChange (code) {
      this.form.setFieldsValue({'zszym':''})
      this.$set(this.selectMenu, 'zy', [])
      if(code){
        this.getProssion(code)
      }
    },
    xxChange (code) {
      this.form.setFieldsValue({'yjfxm':''})
      this.$set(this.selectMenu,'fx','')
      if(code){
      let params={
        ssyxm:this.form.getFieldsValue().zsyxm,
        sszym:this.form.getFieldsValue().zszym,
        xwccm:2,
        zsxn:this.postParams.zsnj,
        qrzhfqrz:code
      }
      this.$api.base.researchAll(params).then(res=>{
        this.$set(this.selectMenu,'fx',res.data)
      })
      }
    },
    getProssion(code){
      this.$api.base.majorAll({lsdwh: code}).then(res => {
        this.$set(this.selectMenu, 'zy', res.data)
      })
    },
    // 关闭
    onClose () {
      // this.form.resetFields()
      this.$emit('close')
      this.form.setFieldsValue({'zszym':''})
       this.postParams= {
        zsyxm:"",
        zszym:"",
        isqrz:"",
        kmyfsx:0,
        kmefsx:0,
        kmsfsx:0,
        kmffsx:0,
        zffsx:0
      },
      this.initSelectMenu()
    },
    // 保存
    handleSubmit () {
      this.postParams.zffsx = this.zffsx
      var params = Object.assign(this.postParams,this.form.getFieldsValue())
      this.$api.recruitstat.reExamCoreLineMangegeAdd(params).then(res => {
        this.$emit('success')
        this.postParams= {
        zsyxm:"",
        zszym:"",
        isqrz:"",
        kmyfsx:0,
        kmefsx:0,
        kmsfsx:0,
        kmffsx:0,
        zffsx:0
      }
        this.form.resetFields()
        this.initSelectMenu()
      }).finally(() => {
        this.loadingBtn = false
      })
    },
    handleDelete (key) {
      const dataSource = [...this.data]
      this.data = dataSource.filter(item => item.key !== key)
    },
    kmyChange(value){
      this.postParams.kmyfsx = value
      console.log('科目一改变',this.postParams.kmyfsx)
    },
    kmeChange(value){
      this.postParams.kmefsx = value
    },
    kmsChange(value){
      this.postParams.kmsfsx = value
    },
    kmrChange(value){
      this.postParams.kmffsx = value
    },
  }
}
</script>
